package com.xqls.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import com.xqls.bean.UserRoleKeyBean;
import com.xqls.biz.BlockBiz;
import com.xqls.biz.DictionaryBiz;
import com.xqls.biz.RoleBiz;
import com.xqls.biz.UserRoleKeyBiz;
import com.xqls.util.CommonPage;
import com.xqls.util.CommonResult;
import com.xqls.util.DictionaryEnums;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/userRoleController")
public class UserRoleController {
	
	@Autowired private RoleBiz roleBiz;

	@Autowired private BlockBiz blockBiz;
	
	@Autowired private DictionaryBiz dictionaryBiz;
	
	@Autowired private UserRoleKeyBiz userRoleBiz;
	
	/**
	 * 访问src/mainwebapp/WEB_INF/views/velocity/templates/userRole/userRoleList.vm
	 * @return ModelAndView
	 */
	@RequestMapping("/toPage")
	public ModelAndView toPage(){
		ModelAndView view =new ModelAndView("/userRole/userRoleList");
		view.addObject("blockList", this.blockBiz.queryList(false));
		view.addObject("dictionaryList", this.dictionaryBiz.getListByKeys(DictionaryEnums.getLevelList()));
		view.addObject("roleList",this.roleBiz.getList());
		return view;
	}
	
	/**
	 * 访问src/mainwebapp/WEB_INF/views/velocity/templates/userRole/userRoleAdd.vm
	 * @return ModelAndView
	 */
	@RequestMapping("/toInsertPage")
	public ModelAndView toInsertPage(){
		ModelAndView view =new ModelAndView("/userRole/userRoleAdd");
		view.addObject("blockList", this.blockBiz.queryList(false));
		view.addObject("dictionaryList", this.dictionaryBiz.getListByKeys(DictionaryEnums.getLevelList()));
		view.addObject("roleList",this.roleBiz.getList());
		return view;
	}
	
	/**
	 * 访问src/mainwebapp/WEB_INF/views/velocity/templates/userRole/userRoleEdit.vm
	 * @return ModelAndView
	 */
	@RequestMapping("/toEditPage")
    public ModelAndView getUserRole(@RequestParam String userId){
		ModelAndView view =new ModelAndView("/userRole/userRoleEdit");
		view.addObject("userRoleBean", this.userRoleBiz.getUserRoleBeanById(userId));
		view.addObject("blockList", this.blockBiz.queryList(false));
		view.addObject("dictionaryList", this.dictionaryBiz.getListByKeys(DictionaryEnums.getLevelList()));
		view.addObject("roleList", this.roleBiz.getList());
		return view;
    }
	
	/**
	 * 插入账户角色信息
	 * @param map
	 * @return JSON
	 */
	@RequestMapping("/insertUserRole")
    @ResponseBody
    public String insertUserRole(@RequestBody Map<String,Object> map){
    	CommonResult commonResult = this.userRoleBiz.insertUserRole(map);

    	return JSONObject.toJSONString(commonResult);
    }
    
	/**
	 * 修改账户角色信息
	 * @param map
	 * @return JSON
	 */
    @RequestMapping("/updateUserRole")
    @ResponseBody
    public String updateUserRole(@RequestBody Map<String,Object> map){
    	CommonResult commonResult = this.userRoleBiz.updateUserRole(map);
    	return JSONObject.toJSONString(commonResult);
    }
    
    /**
     * 删除账户角色信息
     * @param userRoleKeyBean
     * @return JSON
     */
    @RequestMapping("/deleteUserRole")
    @ResponseBody
    public String deleteUserRole(UserRoleKeyBean userRoleKeyBean){
    	CommonResult commonResult = this.userRoleBiz.deleteUserRole(userRoleKeyBean);
    	return JSONObject.toJSONString(commonResult);
    }
    
    /**
     * 批量删除账户角色信息
     * @param keyBeanList
     * @return JSON
     */
    @RequestMapping("/deleteUserRoleList")
    @ResponseBody
    public String deleteUserRoleList(@RequestBody List<UserRoleKeyBean> keyBeanList){
    	CommonResult commonResult = this.userRoleBiz.deleteByList(keyBeanList);
    	return JSONObject.toJSONString(commonResult);
    }
    
    /**
     * 分页显示账户角色信息
     * @param commonPage
     * @return CommonResult
     */
	@RequestMapping("/getList")
	@ResponseBody
	public CommonResult getList(@RequestBody CommonPage commonPage, HttpServletRequest request, HttpServletResponse response){
		CommonResult result = this.userRoleBiz.getListByPage(commonPage,request,response);
		if (result.getIsSuccess()) {
			result.setData(commonPage);
		}
		return result;
	}
}
